package org.ruslan.kolobok.model;

import org.ruslan.kolobok.model.Soldier.SoldierState;



public class SoldierBehaviour implements Behaviour {

	@Override
	public void update(AliveLevelObject<? extends Type> object, float deltaTime) {
		Soldier soldier = (Soldier) object;

		float cumulativeTme = object.getStateTime();
		cumulativeTme += deltaTime;
		
		SoldierState state = (SoldierState) object.getState();
		
		if (state != SoldierState.DIE && soldier.getHealth() < 1) {
			soldier.setState(SoldierState.DIE);
			cumulativeTme = 0;
		}
		
		switch (state) {
		case DIE:
			if (object.getStateTime() == 0) {
				object.getVelocity().set(0, 0, 0);
				object.setStateTime(cumulativeTme);
			}
			else if (object.getStateTime() > 1) {
				object.setActive(false);
			}
			else {
				object.setStateTime(cumulativeTme);
			}
			break;
		case SHOOT:
			if (object.getStateTime() > 1) {
				object.setStateTime(0);
				object.setState(SoldierState.WALK);
			}
			else {
				object.getVelocity().set(0, 0, 0);
				object.setStateTime(cumulativeTme);
			}
			break;
		case WALK:
			if (object.getStateTime() == 0) {
				object.getVelocity().x = -1;//(Math.random() > 0.5 ? 1 : -1);
				object.setStateTime(cumulativeTme);
				object.getVelocity().nor().mul(soldier.getSpeed());
			}
			else if (cumulativeTme > Math.random() * 100) { // seconds
				object.setStateTime(0);
				if (Math.random() > 0.5) {
					object.setState(SoldierState.SHOOT);
				}
				else {
					object.getVelocity().x = -1;//(Math.random() > 0.5 ? 1 : -1);
	//				object.getVelocity().z = (Math.random() > 0.5 ? 1 : -1);
					object.getVelocity().nor().mul(soldier.getSpeed());
				}
			}
			else {
				object.setStateTime(cumulativeTme);			
			}
			break;			

		default:
			break;
		}
	}
}
